//
//  ByUICompensateVC.m
//  chequanbao_ios
//
//  Created by pro on 17/6/6.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUICompensateVC.h"

// cell
#import "ByCellCompensate.h"

// api
#import "ByApiSafety.h"

@interface ByUICompensateVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *table;

@property (nonatomic, strong) NSMutableArray *items;

@end

@implementation ByUICompensateVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"理赔";
    [self addLeftBackBarButtonItem];
    
    // 模拟数据
//    [self.items addObject:@""];
//    [self.items addObject:@""];
//    [self.items addObject:@""];
//    [self.items addObject:@""];
//    [self.items addObject:@""];
//    [self.items addObject:@""];
//    [self.items addObject:@""];
//    [self.items addObject:@""];
//    [self.items addObject:@""];

    [self createTableView];
    
    [self getCompensateList];
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
}

#pragma mark - 请求数据
- (void)getCompensateList {
    
    [ByApiSafety getCompensateList:[ByNetParamsModel new] success:^(ByNetBaseResult *result) {
        
        [self.items addObjectsFromArray:result.data];
        
        [self.table reloadData];
        
    } failure:^(ByNetBaseResult *result) {
        
        [ByCoreToolAlert showFailureWithText:@"获取失败"];
    }];
}
#pragma mark - 懒加载
- (NSMutableArray *)items {
    
    if (!_items) {
        _items = [NSMutableArray new];
    }
    return _items;
}

#pragma mark - 创建视图
// 创建表视图
- (void)createTableView {
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH - 20, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    self.table.backgroundColor = [UIColor clearColor];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    
    self.table.tableFooterView = [UIView new];
}

#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.items.count;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ByCellCompensate *cell = [tableView dequeueReusableCellWithIdentifier:@"ByCellCompensate"];
    if (cell == nil) {
        cell = [[ByCellCompensate alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ByCellCompensate"];
    }
    
    cell.dataDic = self.items[indexPath.section];
    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

// 设置组尾高
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dic = self.items[indexPath.section];
    NSString *tel = [NSString stringWithFormat:@"telprompt://%@",dic[@"contact_number"]];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tel]];
    });
}


@end
